package com.langsin.Search;

import java.util.LinkedList;
import java.util.Queue;


/**
 * @Auther: MFM
 * @Date: 2021/8/19 - 08 - 19 - 18:38
 * @version: 1.0
 */

public class BinarySearchTree<E extends Comparable> {

    /**
     * 节点内部类
     */
    private class Node {
        public E e;
        public Node left;
        public Node right;

        public Node(E e) {
            this.e = e;
            this.left = null;
            this.right = null;
        }
    }

    private Node root;
    private int size;

    public BinarySearchTree() {
        root = null;
        size = 0;
    }

    public int getSize() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    //增加
    public void add(E e) {
        root = add(root, e);
    }

    private Node add(Node node, E e) {

        if (node == null) {
            size++;
            new Node(e);
        }

        if (e.compareTo(node.e) < 0) {
            node.left = add(node.left, e);
        } else if (e.compareTo(node.e) > 0) {
            node.right = add(node.right, e);
        }

        //相等不考虑
        return node;
    }

    //查询
    public boolean contains(E e) {
        return contains(root, e);
    }

    public boolean contains(Node node, E e) {
        if (node == null) {
            return false;
        }

        if (e.compareTo(node.e) == 0) {
            return true;
        } else if (e.compareTo(node.e) > 0) {
            return contains(node.right, e);
        } else {
            return contains(node.left, e);
        }

    }

    public void levelOrder() {
        //先判断是否为空
        if (root == null) {
            throw new IllegalArgumentException("树为空，无法层序遍历");
        }

        //外部队列
        Queue<Node> q = new LinkedList<>();

        //首先把根节点放入队列，然后时刻判断队列元素是否为空，如果不为空
        //移除队首元素，同时使队首的左右子节点入队
        q.add(root);
        while (!q.isEmpty()) {
            //移除队首元素
            Node current = q.remove();
            //打印队首元素
            System.out.println(current.e);

            //把队首元素的左右子节点放进来
            if (current.left != null) {
                q.add(current.left);
            }
            if (current.right != null) {
                q.add(current.right);
            }
        }
    }

    //以下均为删除元素部分代码

    //找到最小值
    public E minimum() {
        //树为空直接抛异常
        if (size == 0) {
            throw new IllegalArgumentException("树为空，没有最小值");
        }

        //开始查找最小值，返回最小值所在节点
        Node minNode = minimum(root);
        //返回最小值
        return minNode.e;

    }

    private Node minimum(Node node) {
        if (node.left == null) {//最左边
            return node;//当前节点就是最小节点
        }

        return minimum(node.left);
    }

    //删除最小值
    public E removeMin() {
        //找到最小值
        E minimum = minimum();
        //删除最小值
        removeMin();
        //返回最小值
        return minimum;
    }

    //删除以node为根的二分搜索树的最小节点，返回删除节点后二分搜索树的根
    private Node removeMin(Node node) {
        //删除最小值，前提就是最小值没有左子树
        //把右子树作为删除节点的父节点的左子树

        if (node.left == null) {
            Node rightNode = node.right;
            node.right = null;
            size--;
            return rightNode;
        }
        node.left = removeMin(node.left);
        return node;
    }


    public E maximum(){
        if (size == 0) {
            throw new IllegalArgumentException("树为空，没有最大值");
        }

        return maximum(root).e;
    }

    private Node maximum(Node node){
        if (node.right==null){
            return node;
        }
        return maximum(node.left);
    }

    public E removeMax(){
        E maximum = maximum();
        root = removeMax(root);
        return maximum;
    }

    private Node removeMax(Node node){
        if (node.right==null){
            Node leftNode = node.left;
            node.left = null;
            size--;
            return leftNode;
        }

        node.right = removeMax(node.right);
        return node;
    }

    public void remove(E e){
        root = remove(root, e);
    }

    //递归注意三点：返回什么，参数变化，递归终止条件

    /**
     * 删除以node为根的二分搜索树中值为e的节点
     * @param node
     * @param e
     * @return  删除节点后二分搜索树的根
     */
    private Node remove(Node node,E e){
        //如果节点为空
        if(node==null){
            return null;
        }

        if (e.compareTo(node.e)>0){
            node.right = remove(node.right, e);
            return node;
        }else if (e.compareTo(node.e)<0) {
            node.left = remove(node.left, e);
            return node;
        }else {//此处重要 ==
            //此处的node已经是待删除的node了，不需要往下递归了
            //1.左子树为空
            if (node.left==null){
                //把右子树提上来，作为父节点的右子树
                Node rightNode = node.right;
                node.right = null;
                //引用更新完成，可以进行垃圾回收 GC
                size--;
                return rightNode;
            }
            //2.右子树为空
            if (node.right==null){
                Node leftNode = node.left;
                node.left = null;
                size--;
                return leftNode;
            }

            //3.左子树和右子树均不为空
            //左子树的最大值或者右子树的最小值来代替

            Node minimumRight = minimum(node.right);
            //删除了右子树的最小值和右子树要变成最小值的右子节点
            minimumRight.right = removeMin(node.right);
            //待删除节点的左子树也要变成最小值的左子树
            minimumRight.left= node.left;
            //待删除节点的左右子树节点均指向null
            node.left=null;
            node.right= null;
            return minimumRight;
        }
    }





}
